package bg.uni_sofia.conf_manager.beans;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MultivaluedMap;

import bg.uni_sofia.conf_manager.model.Conference;
import bg.uni_sofia.conf_manager.model.Lecture;
import bg.uni_sofia.conf_manager.model.Lecturer;


@Stateless
@Path("/lmessages")
public class LecturerMassages {
	
	@PersistenceContext
	private EntityManager em;
	
	@Inject LoginBean sessionData;
	
	
	public LecturerMassages(){
		
	}
	
	@POST
    @Path("/messagesMobile")
    @Consumes("application/x-www-form-urlencoded")
    @Produces("application/json")
    public List<Lecture> getLecturesMessagesMobile(
                    MultivaluedMap<String, String> data) {
            TypedQuery<Lecture> lectureQuery = em.createNamedQuery(
                            "getLecturesByLecturer", Lecture.class);
            lectureQuery.setParameter("id", Long.parseLong(data.get("id").get(0)));
            List<Lecture> lectureList = lectureQuery.getResultList();

            TypedQuery<Lecturer> lecturerLoginQuery = em.createNamedQuery(
                            "getLecturerById", Lecturer.class);
            lecturerLoginQuery.setParameter("id",
                            Long.parseLong(data.get("id").get(0)));
            List<Lecturer> lecturer = lecturerLoginQuery.getResultList();

            List<Lecture> lecturesForMassage = new ArrayList<Lecture>();
            if (lectureList.size() != 0) {
                    for (Lecture l : lectureList) {
                            if (lecturer.get(0).getLastLogin()
                                            .compareTo(l.getLastModified()) < 0
                                            && l.getStatus() != 0) {
                                    lecturesForMassage.add(l);
                            }
                    }
            }
            return lecturesForMassage;
    } 
	
	@GET
	@Path("/approvedOrNot")
	@Produces("application/json")
	public List<Lecture> getLectureMassages() {
		TypedQuery<Lecture> lectureQuery = em.createNamedQuery("getLecturesByLecturer", Lecture.class);
    	lectureQuery.setParameter("id", sessionData.getId());
    	List<Lecture> lectureList = lectureQuery.getResultList();
    	
    	TypedQuery<Lecturer> lecturerLoginQuery = em.createNamedQuery("getLecturerById", Lecturer.class);
    	lecturerLoginQuery.setParameter("id", sessionData.getId());
    	List<Lecturer> lecturer = lecturerLoginQuery.getResultList();
    	
    	List<Lecture> lecturesForMassage = new ArrayList<Lecture>();
    	if(lectureList.size()!=0){
    		for(Lecture l: lectureList){
        		if(lecturer.get(0).getLastLogin().compareTo(l.getLastModified()) < 0 && l.getStatus()!=0){
        			lecturesForMassage.add(l);
        		}
        	}
    	}
    	return lecturesForMassage;
	}
	
	
	@GET
	@Path("/newConferencesForMe")
	@Produces("application/json")
	public List<Conference> getConferenceMassages() {
		TypedQuery<Lecturer> lecturerLoginQuery = em.createNamedQuery("getLecturerById", Lecturer.class);
    	lecturerLoginQuery.setParameter("id", sessionData.getId());
    	List<Lecturer> lecturer = lecturerLoginQuery.getResultList();
		TypedQuery<Conference> confQuery = em.createNamedQuery("getAllConferences", Conference.class);
    	List<Conference> conferences = confQuery.getResultList();
    	List<Conference> newConferences = new ArrayList<Conference>();
    	
    	if(conferences.size()!=0){
    		for (Conference conf: conferences) {
				if(lecturer.get(0).getLastLogin().compareTo(conf.getCreationDate())<0){
					newConferences.add(conf);
				}	
			}
    	}
    	
    	return newConferences;	
	}

	
	@GET
	@Path("/notAddedToConf")
	@Produces("application/json")
	public List<Conference> getNoConferenceMassages() {	
		TypedQuery<Conference> lecturerLoginQuery = em.createNamedQuery("getAllConferences", Conference.class);
		List<Conference> allConferences = lecturerLoginQuery.getResultList();
    	boolean isAdded = false;
    	Date today = new Date();
    	List<Conference> notAddedConferences = new ArrayList<Conference>();
    	if(allConferences.size()!=0){
    		for (Conference conf: allConferences) {   			
    			long timeDiff = conf.getStartTime().getTime() - today.getTime();
    			long timeDiffDays = timeDiff/(1000 * 60 * 60 * 24); 
    			if (timeDiffDays <= 45 && timeDiffDays >=30) {
    				List<Lecturer> lecturersToConf = conf.getLecturers();
    				if (lecturersToConf.size()!=0) {	
    					for (Lecturer lec: lecturersToConf) {					
    						if(lec.getId() == sessionData.getId()){
    							isAdded = true;
    							break;
    						}	
    					}
    					if(!isAdded){
    						notAddedConferences.add(conf);
    					}
    					isAdded = false;
    				}else{
    					notAddedConferences.add(conf);
    				}
				}	
			}
				
		}
    	
    	return notAddedConferences;	
	}
	
}
